package com.servlet;

import com.alibaba.fastjson.JSONObject;
import com.entity.CommonResult;
import com.service.IClassInfoService;
import com.service.impl.ClassInfoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

@WebServlet("/ClassInfoServlet/*")
public class ClassInfoServlet extends HttpServlet {
    //业务层对象
    private IClassInfoService classInfoService=new ClassInfoServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uri=req.getRequestURI();
        String process=uri.substring(uri.lastIndexOf("/")+1);
        //定义结果对象
        Object date=null;
        switch (process){
            case "query":
                date=query(req,resp);
                break;
        }

        //输出json字符串
        String jsonStr= JSONObject.toJSONString(date);
        PrintWriter out=resp.getWriter();
        out.println(jsonStr);
        out.flush();
        out.close();

    }

    //查询
    private Object query(HttpServletRequest req, HttpServletResponse resp) {
        List<Map<String,Object>> clsList = classInfoService.listAll();
        return CommonResult.success(clsList.size(), clsList);
    }
}
